<template>
    <div>
        <a-form layout="inline" :model="searchData" style="margin-top: 10px">
            <a-form-item>
                <a-input v-model:value="searchData.name" placeholder="请输入废品名称"></a-input>
            </a-form-item>
            <a-form-item>
                <a-input v-model:value="searchData.code" placeholder="请输入废品编号"></a-input>
            </a-form-item>
            <a-form-item>
                <a-select v-model:value="searchData.parentId"
                          placeholder="请选择所属类型"
                          :options="wasteTypeList"
                          :field-names="{label: 'name', value: 'id'}">
                </a-select>
            </a-form-item>
            <a-form-item>
                <a-space>
                    <a-button type="primary" @click="load(1)">搜索</a-button>
                    <a-button type="default" @click="reset">重置</a-button>
                </a-space>
            </a-form-item>
        </a-form>
        <a-button type="primary" style="margin-top: 10px" @click="openModal({})">新增</a-button>
        <a-table :dataSource="wasteList"
                 style="margin-top: 10px"
                 :columns="columns"
                 :pagination="false"
                 bordered>
            <template #action="{text,record}">
                <a-space>
                    <a-button type="primary" @click="openModal(record)">编辑</a-button>
                    <a-popconfirm
                            title="是否确认删除?"
                            ok-text="是"
                            cancel-text="否"
                            @confirm="okDelete(record.id)"
                    >
                        <a-button type="danger">删除</a-button>
                    </a-popconfirm>
                </a-space>
            </template>
        </a-table>
        <a-config-provider :locale="locale">
            <a-pagination style="margin-top: 10px; float: right"
                          v-model:current="pagination.current"
                          v-model:pageSize="pagination.pageSize"
                          show-quick-jumper
                          show-size-changer
                          :total="pagination.total"
                          :show-total="total => `总 ${total} 条数据`"
                          :page-size-options="pageSizeOptions"
                          @change="onChange"/>
        </a-config-provider>
        <a-modal v-model:visible="visible" title="新增或编辑" @ok="handleOk">
            <a-form v-model="waste"
                    :label-col="{ span: 8 }">
                <a-form-item label="废品名称">
                    <a-input v-model:value="waste.name"></a-input>
                </a-form-item>
                <a-form-item label="废品编号">
                    <a-input v-model:value="waste.code"></a-input>
                </a-form-item>
                <a-form-item label="所属类型">
                    <a-select v-model:value="waste.parentId"
                              placeholder="请选择所属类型"
                              :options="wasteTypeList"
                              :field-names="{label: 'name', value: 'id'}">
                    </a-select>
                </a-form-item>
            </a-form>
        </a-modal>
    </div>
</template>

<script>
    import {ref, onMounted} from 'vue'
    import axios from 'axios'
    import dayjs from 'dayjs';
    import {message} from 'ant-design-vue';
    import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN'
    export default {
        name: "admin-waste",
        setup() {
            const columns = [
                {
                    title: 'ID',
                    dataIndex: 'id',
                    key: 'id',
                    align: 'center',
                },
                {
                    title: '废品名称',
                    dataIndex: 'name',
                    key: 'name',
                    align: 'center',
                },
                {
                    title: '废品编号',
                    dataIndex: 'code',
                    key: 'code',
                    align: 'center'
                },
                {
                    title: '所属类型',
                    dataIndex: 'wasteTypeName',
                    key: 'wasteTypeName',
                    align: 'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    slots: {customRender: 'action'},
                    align: 'center',
                    fixed: 'right',
                    width: '200px'
                }
            ]
            const locale = zhCN
            const waste = ref({})
            const wasteList = ref()
            const wasteTypeList = ref()
            const searchData = ref({})
            const pagination = ref({
                current: 1,
                pageSize: 5,
            })
            const pageSizeOptions = ref(['5', '10', '20', '30', '50', '100'])
            const visible = ref(false);

            const handleOk = () => {
                axios.post('/waste/saveOrUpdate', waste.value).then(rs => {
                    console.log("新增或修改结果：", rs)
                    if (rs.data.code == 200) {
                        visible.value = false
                        message.success("操作成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }

            const okDelete = (id) => {
                console.log("要删除的ID：" + id)
                axios.delete('/waste/delete/' + id).then(rs => {
                    if (rs.data.code == 200) {
                        message.success("删除成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }

            const openModal = (obj) => {
                console.log("模态框参数：", obj)
                visible.value = true;
                waste.value = JSON.parse(JSON.stringify(obj))
            }

            const load = (pageNum) => {
                pagination.value.current = pageNum == 1 ? 1 : pagination.value.current
                axios.get('/waste/list', {
                    params: {
                        pageNum: pagination.value.current,
                        pageSize: pagination.value.pageSize,
                        name: searchData.value.name,
                        code: searchData.value.code,
                        parentId: searchData.value.parentId
                    }
                }).then(rs => {
                    console.log("查找结果：", rs)
                    wasteList.value = rs.data.data.records
                    pagination.value.total = rs.data.data.total
                })
            }

            const onChange = (pageNum, pageSize) => {
                pagination.value.current = pageNum
                pagination.value.pageSize = pageSize
                load()
            }

            const reset = () => {
                searchData.value = {}
                load()
            }

            onMounted(() => {
                axios.get('waste-type/queryAll').then(rs=>{
                    wasteTypeList.value = rs.data.data
                })
                load()
            })
            return {
                columns,
                waste,
                wasteList,
                pagination,
                pageSizeOptions,
                onChange,
                load,
                locale,
                searchData,
                reset,
                visible,
                openModal,
                handleOk,
                okDelete,
                wasteTypeList
            }
        }
    }
</script>

<style scoped>

</style>